<?php
header("Content-type: text/html; charset=utf-8");

//设置项目路径
define('KJ_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
define('KJ_PATH_PRE', realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..').DIRECTORY_SEPARATOR);

//设置文件引入路径
ini_set('include_path', KJ_PATH);

class KJ {
	static $_model;

	static function start() {
		$controller = self::controller().'Controller';
		$action = 'action'.self::action();

		self::runBaseFile();

		$runController['controller'] = $controller;
		self::runBaseFile($runController);

		self::init($controller, $action);
	}

	static function init($controller, $action) {

		if (method_exists($controller, $action)) {
			$mvc_run = new $controller;
			$mvc_run->$action(self::params());
		} else {
			shutDown("没有找到控制器\"".$controller."\"中的方法\"".$action."\"");
		}

	}

	/*
	 * 获取controller名称
	 */
	static function controller() {
		$controller = !empty($_GET["controller"]) ? htmlspecialchars($_GET["controller"]) : "Index";
		return ucfirst(strtolower($controller));
	}

	/*
	 * 获取controller名称
	 */
	static function action() {
		$action = !empty($_GET["action"]) ? htmlspecialchars($_GET["action"]) : "Index";
		
		return ucfirst(strtolower($action));
	}

	/*
	 * 处理$_GET参数
	 */
	static function params() {
		$params = !empty($_GET["parameters"]) ? htmlspecialchars($_GET["parameters"]) : "";
		$params_arr = array();

		if (!empty($params)) {
			$param_t = explode("/", $params);
			$param_n = 0;
			$param_k = null;

			foreach ($param_t as $key => $value) {
				$params_m = $param_n % 2;

				if ($params_m == 0) {
					$param_k = trim($value); //求模，偶数为新数组键
					$param_n++;
					continue;
				}
				
				$params_arr[$param_k] = trim($value);
				$param_n++;
			}
		}

		unset($_GET['parameters'], $param_t, $params_m, $param_n);
		$params_arr = array_merge($params_arr, $_GET);

		return $params_arr;
	}

	static function runBaseFile(array $files=array(), $dir='') {

		if(empty($files))
			$files = array(
				'config' => 'config',					//加载配置文件
				'libs' => array(
					'mysql_new',			//引入公共数据库类
					'global.function',					//引入全局方法
					'BaseModel',			//引入全局方法
					'BaseController',					//引入全局方法
				),
			);

		foreach ($files as $key => $value) {
			if(is_array($value)) {
				self::runBaseFile($value, ltrim($dir.DIRECTORY_SEPARATOR.$key, DIRECTORY_SEPARATOR));
			}
			else { 
				$path = empty($dir) ? $key : $dir;
				$require_file = KJ_PATH.$path.DIRECTORY_SEPARATOR.$value.'.php';

				if(file_exists($require_file)) {
					require_once $require_file;
				} else {
					shutDown($require_file.'不存在');
				}				
			}
		}
	}

	static function instance($file, $model) {
		if(file_exists($file)) {
			require_once $file;
			
			return new $model;
		} else {
			shutDown($file.'实例不存在');
		}
	}

	static function M($model) {
		$model = ucfirst(strtolower($model)).'Model';

		$modelFile = KJ_PATH.'model'.DIRECTORY_SEPARATOR.$model.'.php';

		$fileExists = file_exists($modelFile);
		if(!isset(static::$_model[$model]) && $fileExists) {
			self::runBaseFile(array('model'=>$model));

			static::$_model[$model] = new $model;
		} else if(!$fileExists) {
			shutDown($model.'模型不存在');
		}

		return static::$_model[$model];
	}
}

KJ::start();
